import { Component, ChangeDetectionStrategy, OnInit, ViewChild, ChangeDetectorRef, inject } from '@angular/core';
import { STPage, STComponent, STColumn, STChange, STModule } from '@delon/abc/st';
import { _HttpClient } from '@delon/theme';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzTagModule } from 'ng-zorro-antd/tag';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
import { Api } from 'src/app/shared/api/api';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-account-profile-consent',
  templateUrl: './user-consent.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [NzCardModule, STModule, NzTagModule, NzToolTipModule]
})
export class UserConsentComponent implements OnInit {
  protected user: NzSafeAny;
  protected securityInfo: NzSafeAny = {};
  protected readonly http = inject(_HttpClient);
  private readonly msg = inject(NzMessageService);
  protected readonly modalService = inject(NzModalService);
  private readonly changeDetectorRef = inject(ChangeDetectorRef);

  params: NzSafeAny = {};
  page: NzSafeAny = {
    records: [],
    current: 1,
    total: 0,
    size: 10
  };
  pagination: STPage = {
    front: false,
    pageSizes: [10, 20, 30, 40, 50],
    total: true,
    showSize: true,
    showQuickJumper: true
  };

  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: 'clientId', width: '200px', index: 'registeredClientId' },
    { title: '名称', index: 'registeredClientName', width: 300 },
    { title: '权限', render: 'authoritiesTags' },
    {
      title: '操作',
      width: 150,
      buttons: [
        {
          text: '解除',
          icon: 'delete',
          click: (record: NzSafeAny) => {
            this.delete(record);
          }
        }
      ]
    }
  ];

  ngOnInit(): void {
    this.query();
  }

  change(e: STChange) {
    if (e.type === 'pi' || e.type === 'ps') {
      this.params.size = e.ps;
      this.params.current = e.pi;
      this.query();
    }
  }

  query() {
    const current: number = this.params.current || 1;
    const size: number = this.params.size || 10;
    this.http.get(`${Api.BaseClientApi}/user/consent/${current}/${size}`).subscribe((res: NzSafeAny) => {
      if (res && res.code === ResponseCode.SUCCESS) {
        if (res.data) {
          this.page = res.data;
        } else {
          this.page = { records: [], current: 1, total: 0, size: 10 };
        }
        this.changeDetectorRef.markForCheck();
        this.changeDetectorRef.detectChanges();
      }
    });
  }

  delete(record: NzSafeAny) {
    this.modalService.confirm({
      nzTitle: '确定解除吗?',
      nzContent: '<b style="color: red;">如果您确定要接触对该客户端的授权请点击确定按钮，否则点取消</b>',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: () =>
        this.http.delete(`${Api.BaseClientApi}/user/consent?clientId${record.registeredClientId}`).subscribe((res: NzSafeAny) => {
          if (res) {
            if (res.code === ResponseCode.SUCCESS) {
              this.st.reload();
              this.msg.success('解除成功');
            } else {
              this.msg.warning(res.message);
            }
          } else {
            this.msg.error('解除失败，未知错误');
          }
        }),
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel')
    });
  }
}
